//
//  YSZCommentViewController.swift
//  GZWeibo666
//
//  Created by ysz on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

import SVProgressHUD

protocol YSZCommentViewControllerdelagte : NSObjectProtocol
{
   func reflash()
}

enum YSZCommentViewControllerType: Int
{
    ///评论枚举
    case comments = 1
    ///转发枚举
    case transmit = 2
}

class YSZCommentViewController: UIViewController {

    weak var delagate: YSZCommentViewControllerdelagte?
    var status: CZStatus?
    
    private var commentViewType: YSZCommentViewControllerType
    
    init(commentType: YSZCommentViewControllerType, statu: CZStatus)
    {
        status = statu
        self.commentViewType = commentType
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func loadView() {
        super.loadView()
        prepareUI()
    }
    
    private func prepareUI()
    {
        view.addSubview(textView)
        
        textView.ff_Fill(view)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        //导航栏左边取消按钮＋点击事件
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        //右边按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "setSataus")
        
        setTitle()
        
    }
    ///设置title
    private func setTitle()
    {
        var titleStr: String?
        if commentViewType == YSZCommentViewControllerType.comments
        {
            titleStr = "评论"
        }
        else if commentViewType == YSZCommentViewControllerType.transmit
        {
            titleStr = "转发"
        }
        if let name = CZUserAccount.loadUserAccount()?.screen_name {
            // 有用户名
            let titleName = titleStr! + "\n" + name
            
            // 创建可变的属性文本
            let attrString = NSMutableAttributedString(string: titleName)
            
            // 获取NSRange
            let nameRange = (titleName as NSString).rangeOfString(name)
            
            // 设置属性文本的属性
            attrString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(12), range: nameRange)
            attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGrayColor(), range: nameRange)
            
            // 创建label
            let label = UILabel()
            // 设置属性文本
            label.numberOfLines = 0
            label.textAlignment = NSTextAlignment.Center
            label.font = UIFont.systemFontOfSize(14)
            // 顺序不要搞错
            label.attributedText = attrString
            label.sizeToFit()
            
            navigationItem.titleView = label
        } else {
            // 没有用户名
            navigationItem.title = titleStr
        }

    }
    func setSataus()
    {
        let text = textView.text
        if text == nil || text == ""
        {
            SVProgressHUD.showErrorWithStatus("内容不能为空")
            return
        }
        if commentViewType == YSZCommentViewControllerType.comments
        {
            CZNetworkTool.sharedInstance.sendComment(text, id: status!.id, finished: { (result, error) -> () in
                if  error != nil || result == nil
                {
                    SVProgressHUD.showErrorWithStatus("网络不给力")
                    return
                }
                else
                {
                    self.delagate?.reflash()
                    
                }
                SVProgressHUD.showSuccessWithStatus("评论成功")
            })
            self.close()
        }
        else if commentViewType == YSZCommentViewControllerType.transmit
        {
            CZNetworkTool.sharedInstance.repostStatus(status!.id, statusCode: text, finished: { (result, error) -> () in
                if  error != nil || result == nil
                {
                    SVProgressHUD.showErrorWithStatus("网络不给力")
                    return
                }
                else
                {
                    self.delagate?.reflash()
                    SVProgressHUD.showSuccessWithStatus("转发成功")                }
            })
            self.close()
        }
    }
    ///取消事件
    func close()
    {
        dismissViewControllerAnimated(true, completion: nil)
    }
    //懒加载
    private lazy var textView: UITextView =
    {
        let textView = UITextView()
        textView.backgroundColor = UIColor.darkGrayColor()
        
        return textView
    }()
}
